﻿Imports System.Data.Entity
Imports System.Collections.Generic
Imports System.Data.SqlClient


<Authorize>
Public Class SalesPromotionController
    Inherits System.Web.Mvc.Controller

    Public db As New SalesPromotionDBContext

    '
    ' GET: /SalesPromotion/

    Function Index() As ActionResult

        Return View(db.SalesPromotions.ToList())
    End Function

    '
    ' GET: /SalesPromotion/Details/5

    Function Details(Optional ByVal id As Integer = Nothing) As ActionResult
        Dim salespromotion As SalesPromotion = db.SalesPromotions.Find(id)
        If IsNothing(salespromotion) Then
            Return HttpNotFound()
        End If
        Return View(salespromotion)
    End Function

    '
    ' GET: /SalesPromotion/Create

    Function Create() As ActionResult

        ' Create a list of Locations from table dbo.Locations. 
        Dim co As New List(Of SelectListItem)()
        Dim locations As New List(Of Location)()


        Dim cc As MVC1.LocationController = New LocationController()
        locations = cc.db.Locations.ToList()

        For Each item As Location In locations
            co.Add(New SelectListItem() With {.Text = item.Name, .Value = item.ID})
        Next

        ViewBag.ddLocation = co

        Return View()
    End Function

    '
    ' POST: /SalesPromotion/Create

    <HttpPost()> _
    <ValidateAntiForgeryToken()> _
    Function Create(ByVal salespromotion As SalesPromotion) As ActionResult
        If ModelState.IsValid Then

            Dim LocationID As String = Request("ddLocation")
            salespromotion.LocationID = LocationID

            Dim guid As Guid = guid.NewGuid()

            Dim googleChartUrl As String
            googleChartUrl = "http://chart.apis.google.com/chart?cht=qr&chs=300x300&chl=http://www.qrcode-coupons.com//Coupon/Create/?qr=" + guid.ToString()

            Dim imageLocation As String = Server.MapPath("~/Images/QR/") + guid.ToString() + ".jpg"

            My.Computer.Network.DownloadFile(googleChartUrl, imageLocation)

            salespromotion.Code = guid.ToString
            salespromotion.Image = imageLocation

            db.SalesPromotions.Add(salespromotion)
            db.SaveChanges()
            Return RedirectToAction("Index")
        End If

        Return View(salespromotion)
    End Function

    '
    ' GET: /SalesPromotion/Edit/5

    Function Edit(Optional ByVal id As Integer = Nothing) As ActionResult
        Dim salespromotion As SalesPromotion = db.SalesPromotions.Find(id)
        If IsNothing(salespromotion) Then
            Return HttpNotFound()
        End If
        Return View(salespromotion)
    End Function

    '
    ' POST: /SalesPromotion/Edit/5

    <HttpPost()> _
    <ValidateAntiForgeryToken()> _
    Function Edit(ByVal salespromotion As SalesPromotion) As ActionResult
        If ModelState.IsValid Then
            db.Entry(salespromotion).State = EntityState.Modified
            db.SaveChanges()
            Return RedirectToAction("Index")
        End If

        Return View(salespromotion)
    End Function

    '
    ' GET: /SalesPromotion/Delete/5

    Function Delete(Optional ByVal id As Integer = Nothing) As ActionResult
        Dim salespromotion As SalesPromotion = db.SalesPromotions.Find(id)
        If IsNothing(salespromotion) Then
            Return HttpNotFound()
        End If
        Return View(salespromotion)
    End Function

    '
    ' POST: /SalesPromotion/Delete/5

    <HttpPost()> _
    <ActionName("Delete")> _
    <ValidateAntiForgeryToken()> _
    Function DeleteConfirmed(ByVal id As Integer) As RedirectToRouteResult
        Dim salespromotion As SalesPromotion = db.SalesPromotions.Find(id)
        db.SalesPromotions.Remove(salespromotion)
        db.SaveChanges()
        Return RedirectToAction("Index")
    End Function

    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        db.Dispose()
        MyBase.Dispose(disposing)
    End Sub

End Class